

package AddressBook.View.Dialog;

import AddressBook.Model.AuthorizedFileExtension;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;

/**
 * The Class MyFileChooser allows to create a custom fileChooser dialog
 */
public class MyFileChooser extends JFileChooser {

    /**
     *     This field defines the different states taken by the file chooser
     */
    public static enum State {IDLE, OPEN_DIALOG, SAVE_DIALOG}

    /**
     * This field stores the current state
     */
    private State currentState = State.IDLE;

    /**
     * This constructor initialize the fileChooser with the file's extension authorized
     */
    public MyFileChooser() {
        FileNameExtensionFilter fileFilterCsvExtension = new FileNameExtensionFilter(AuthorizedFileExtension.getDottedCsvExtension(),AuthorizedFileExtension.CSV_EXTENSION);
        FileNameExtensionFilter fileFilterSerExtension = new FileNameExtensionFilter(AuthorizedFileExtension.getDottedSerExtension(),AuthorizedFileExtension.SER_EXTENSION);

        addChoosableFileFilter(fileFilterCsvExtension);
        addChoosableFileFilter(fileFilterSerExtension);

        setFileFilter(fileFilterCsvExtension);
    }

    /**
     * This method shows the save dialog
     * @param parent  The parent view
     * @return The state of the dialog
     * @throws HeadlessException
     */
    @Override
    public int showSaveDialog(Component parent) throws HeadlessException {
        currentState = State.SAVE_DIALOG;
        return super.showSaveDialog(parent);    //To change body of overridden methods use File | Settings | File Templates.
    }

    /**
     * This method shows the open dialog
     * @param parent The parent view
     * @return The state of the dialog
     * @throws HeadlessException
     */
    @Override
    public int showOpenDialog(Component parent) throws HeadlessException {
        currentState = State.OPEN_DIALOG;
        return super.showOpenDialog(parent);    //To change body of overridden methods use File | Settings | File Templates.
    }

    /**
     * This method returns the state of the dialog
     * @return The current state
     */
    public State getCurrentState() {
        return currentState;
    }

    /**
     * This method reset the state of the dialog
     */
    public void resetState() {
        currentState = State.IDLE;
    }
}
